<%= include ic/admin-header.html %>

<div class="am-cf admin-main">
  <%= include ic/admin-sidebar.html %>

  <!-- content start -->
  <div class="admin-content">
    <div class="am-cf am-padding">
      <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">用户管理</strong> / <small>编辑页面 - <%= result.get("username") %></small></div>
    </div>

	<div class="am-padding-left">ID:<%= result.id %></div>
    <hr style=" margin-top:0.5rem;" />
	<div class="am-g-fixed am-cf am-padding-left am-padding-bottom">
		<form class="am-form am-u-sm-12 am-form-horizontal am-cf" id="my-form">
			<input id="data-id" type="hidden" value="<%= result.id %>">
			<div class="am-u-sm-12 am-u-md-10 am-fl">
        <div class="am-u-sm-12 am-nbfc am-padding">
          <img class="am-u-sm-12" width="100px" id="imgCover" src="<% if(result && result.get('avatar')){ %><%= result.get('avatar').url() %><% }else{ %>/i/default.png<% } %>" />
        </div>
				<div class="am-form-group">
					<label for="data-title" class="am-u-sm-2 am-form-label">用户名</label>
					<div class="am-u-sm-10">
						<input type="text" id="data-username" value="<%= result.get("username") %>" placeholder="2-20字符" data-foolish-msg="2-20字符的用户名" minlength="2" maxlength="20" required>
					</div>
				</div>

				<div class="am-form-group">
					<label for="data-name" class="am-u-sm-2 am-form-label">邮箱</label>
					<div class="am-u-sm-10">
						<input type="email" id="data-email" value="<%= result.get("email") %>" class="am-form-field<% if(result.get("emailVerified")){ %> am-disabled<% } %>"  placeholder="Email" required>
					</div>
				</div>

				<div class="am-form-group">
					<label for="data-title" class="am-u-sm-2 am-form-label">修改密码</label>
					<div class="am-u-sm-10">
						<input type="text" id="data-password"  placeholder="由英文、数字和下划线组成的密码，长度5-32字符(不修改则不填)"  minlength="0" maxlength="32" pattern="^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]+$">
					</div>
				</div>
			</div>
		</form>
		<hr class="am-u-sm-12 am-fl am-cf" />
		<div class="am-g am-u-sm-8 am-fl am-cf">
			<div class="am-u-sm-offset-2 am-u-sm-3">
				<div class="am-btn am-btn-default" onclick="history.go(-1)">返回列表</div>
			</div>

			<div class="am-u-sm-offset-1 am-u-sm-3 am-u-end">
				<div id="btn-saveEdit" class="am-btn am-btn-primary" onclick="saveEdit(<% if(result){ %>'<%= result.id %>'<% } %>)">确认保存</div>
			</div>
		</div>
		<div class="am-u-sm-12 am-fl am-cf" ></div>
	</div>
  </div>
  <!-- content end -->

</div>

<a class="am-icon-btn am-icon-th-list am-show-sm-only admin-menu" data-am-offcanvas="{target: '#admin-offcanvas'}"></a>

<%= include ic/admin-footer.html %>

<script type="text/javascript">
var validator 	= $('#my-form').validator({submit: function(e) {
	if(this.isFormValid()){
		$.AMUI.progress.start();
		$.post("save", {
			"ajax"		: true,
			"id"		: $('#data-id').val(),
			"email"		: $.trim($('#data-email').val()),
			"username"	: $.trim($('#data-username').val()),
			"displayName"	: $('#data-displayName').val(),
			"password"	: $.trim($('#data-password').val())
		},function(data){
			$.AMUI.progress.done();
			$("#btn-saveEdit").removeClass("am-disabled");
			if(typeof(data)=="object" && typeof(data.error)=="undefined"){
				mAlert({
					title:"提示",
					text:"保存成功！",
					onConfirm:function(e) {
						gotoUrl('list');
					}
				});
			}else{
				console.dir(data);
				mAlert({
					title:"错误",
					text:data.error,
					onConfirm:function(e) {}
				});
			}
		}, "json");
	}else{
		$("#btn-saveEdit").removeClass("am-disabled");
	}
	return false;
}});

function saveEdit(){
	$("#btn-saveEdit").addClass("am-disabled");
	var password=$.trim($("#data-password").val());
	$("#data-password").val(password);
	if(password.length>0){
		$("#data-password").attr("minlength","5");
	}else{
		$("#data-password").attr("minlength","0");
	}
	validator.submit();
}
</script>
